﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using PNProject.EF;

namespace PNProject
{
    public static class Utils
    {
        public static string LocalResources(this WebViewPage page, string key)
        {
            return page.ViewContext.HttpContext.GetLocalResourceObject(page.VirtualPath, key) as string;
        }
        public static bool IsLocalizedUrl(this string url)
        {
            int length = url.Length;
            if (length < 7)
                return false;
            else if (length == 7)
                return url[4] == '-';
            else
                return url.Length > 7 && url[4] == '-' && url[7] == '/';
        }
        public static bool IsLocalizedUrl(this string url, bool rawPath)
        {
            if (rawPath)
            {
                int length = url.Length;
                if (length < 6)
                    return false;
                else if (length == 6)
                    return url[3] == '-';
                else
                    return url.Length > 6 && url[3] == '-' && url[6] == '/';
            }
            else
            {
                return url.IsLocalizedUrl();
            }
        }

        public static string LocalizedRootPath(this System.Web.Mvc.HtmlHelper html)
        {
            HttpRequest request = HttpContext.Current.Request;
            string rawUrl = request.RawUrl;
            if (rawUrl.IsLocalizedUrl(true))
            {
                return rawUrl.Substring(0, 6);
            }
            return string.Empty;
        }

        public static string UnlocalizedRootPath(this System.Web.Mvc.HtmlHelper html)
        {
            HttpRequest request = HttpContext.Current.Request;
            string rawUrl = request.RawUrl;
            if (rawUrl.IsLocalizedUrl(true))
            {
                return rawUrl.Substring(6);
            }
            return rawUrl;
        }

        public static string GetLocalizedCultureCode(this string rawPath)
        {
            return rawPath.Substring(1, 5);
        }
        public static MvcHtmlString MenuItem(this System.Web.Mvc.HtmlHelper helper,
            string linkText, string actionName, string controllerName)
        {
            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var builder = new TagBuilder("li");
            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase)
                && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                builder.AddCssClass("current");
            var titleLike = linkText;
            
            builder.InnerHtml = helper.ActionLink(titleLike, actionName, controllerName).ToHtmlString();
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }
        public static MvcHtmlString MenuItemTaga(this System.Web.Mvc.HtmlHelper helper,
            string linkText, string actionName, string controllerName)
        {
            string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            var builder = new TagBuilder("a");
            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase)
                && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                builder.AddCssClass("current");
            var span = new TagBuilder("span");
            span.SetInnerText(linkText);
            UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
            var url = urlHelper.Action(actionName, controllerName);
            builder.Attributes.Add("href", url);
            builder.InnerHtml = span.ToString(TagRenderMode.Normal);//helper.ActionLink(span.(), actionName, controllerName).ToHtmlString();
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
        }

        public static void TrackingUser(string userId=null,string userName=null,string controller=null,string action=null,string route=null,string description=null)
        {
            var track = new UserTracking
                        {
                            UserTrackID = Guid.NewGuid(),
                            Username = userName,
                            Controller = controller,
                            Action = action,
                            Route = route,
                            CreateDate = DateTime.Now,
                            Description = description
                        };
            StaticData.userTrackingRepo.Save(null,track);
        }
    }
}